package com.ajiao.Connection;

import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {

    @Test
    public void testConnection1() throws SQLException {
        //获取Driver实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        //jdbc:mysql：主协议
        //localhost：ip地址
        //3306：默认mysql端口
        //test：其中一数据库
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名密码封装在Properties
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");

        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    //方法二 在如下程序中不出现第三方api  使用反射，使得更好的可移植性
    @Test
    public void testConnection2() throws Exception {
        //获取Driver实现类对象 使用反射来实现
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //要连接的数据库
        String url = "jdbc:mysql://localhost:3306/test";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");
        //获取连接
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    //方法三 DriverManager来替换Driver
    @Test
    public void testConnection3() throws Exception {
        //获取Driver实现类对象 使用反射来实现
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //提供另外三个基本连接信息
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";
        //注册驱动
        DriverManager.registerDriver(driver);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    //方法四 优化
    @Test
    public void testConnection4() throws Exception {
        //提供另外三个基本连接信息
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        //加载Driver
        Class.forName("com.mysql.jdbc.Driver");
//        Driver driver = (Driver) clazz.newInstance();
//        //注册驱动
//        DriverManager.registerDriver(driver);

        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    //方法五 将数据库需要连接的 基本信息放到properties配置文件当中 通过读取配置文件获取
    @Test
    public void testConnection5() throws Exception {
        //读取配置文件中的4个基本信息
        InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        //加载驱动
        Class.forName(driverClass);
        //获取连接
        Connection conn=DriverManager.getConnection(url,user,password);
        System.out.println(conn);

    }

}
